//
//  PhotoManager.m
//
//
//  Created by ff on 2022/8/10.
//

#import "PhotoManager.h"

static PhotoManager * manager = nil;

@interface PhotoManager ()
<HXCustomNavigationControllerDelegate,
UIImagePickerControllerDelegate,
UINavigationControllerDelegate>

@property (nonatomic,strong) HXPhotoManager * manager;

@property (nonatomic,strong) UIImagePickerController * imagePicker;

@property (nonatomic,copy)  void(^finishedImage) (HXPhotoModel *);

@end

@implementation PhotoManager

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (manager == nil) {
            manager = [[PhotoManager alloc]init];
        }
    });
    return manager;
}

- (HXPhotoManager *)manager
{
    if (!_manager) {
        _manager = [[HXPhotoManager alloc]initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.configuration.hideOriginalBtn = YES;
        _manager.configuration.singleSelected = YES;
        _manager.configuration.requestImageAfterFinishingSelection = YES;
        _manager.configuration.type = HXConfigurationTypeWXMoment;
        _manager.configuration.showOriginalBytes = YES;
        _manager.configuration.showOriginalBytesLoading = YES;
        _manager.configuration.selectedTitleColor = RDRGB(0x222222);
        _manager.configuration.cellSelectedTitleColor = RDRGB(0x222222);
        _manager.configuration.bottomDoneBtnBgColor = UIColor.blueColor;
        _manager.configuration.navBarTranslucent = NO;
        _manager.configuration.singleJumpEdit = YES;
        _manager.configuration.photoEditConfigur.onlyCliping = YES;
        _manager.configuration.navigationBar = ^(UINavigationBar *navigationBar, UIViewController *viewController) {
            
        };
        _manager.selectPhotoFinishDismissAnimated = NO;
        _manager.cameraFinishDismissAnimated = NO;
    }
    return _manager;
}

- (HXCustomNavigationController*)picker
{
    HXCustomNavigationController * picker = [[HXCustomNavigationController alloc]initWithManager:self.manager delegate:self];
    picker.modalPresentationStyle = UIModalPresentationFullScreen;
    picker.modalPresentationCapturesStatusBarAppearance = YES;
    return picker;
}

- (UIImagePickerController*)imagePicker
{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}

#pragma mark - Private

- (void)takePhoto:(UIViewController*)viewController
         finished:(void(^)(HXPhotoModel *))completion
{
    [self.class checkAccessForPhoto:^(BOOL enabled) {
        if (enabled) {
            self.manager.configuration.photoEditConfigur.aspectRatio = HXPhotoEditAspectRatioType_1x1;
            [self.manager setType:HXPhotoManagerSelectedTypePhoto];
            self.finishedImage = completion;
            HXCustomNavigationController * vc = [self picker];
            vc.isCamera = NO;
            [viewController presentViewController:vc animated:YES completion:nil];
        }else {
        }
    }];
}

- (void)takeIdPhoto:(UIViewController*)viewController
           finished:(void(^)(HXPhotoModel *))completion{
    
    [self.class checkAccessForPhoto:^(BOOL enabled) {
        if (enabled) {
            self.manager.configuration.photoEditConfigur.aspectRatio = HXPhotoEditAspectRatioType_Custom;
            self.manager.configuration.photoEditConfigur.customAspectRatio = CGSizeMake(375 * FMScaleX, 250 * FMScaleX);
            [self.manager setType:HXPhotoManagerSelectedTypePhoto];
            self.finishedImage = completion;
            HXCustomNavigationController * vc = [self picker];
            vc.isCamera = NO;
            [viewController presentViewController:vc animated:YES completion:nil];
        }else {
        }
    }];
    
    
}

- (void)takeCamera:(UIViewController*)viewController
          finished:(void(^)(HXPhotoModel *))completion
{
    [self.class checkAccessForCamera:^(BOOL enabled) {
        if (enabled) {
            [self.manager setType:HXPhotoManagerSelectedTypePhoto];
            self.finishedImage = completion;
            HXCustomNavigationController * vc = [self picker];
            vc.isCamera = YES;
            [viewController presentViewController:vc animated:YES completion:nil];
        }else {
        }
    }];
}


- (void)takeVideo:(UIViewController*)viewController
         finished:(void(^)(HXPhotoModel *))completion
{
    [self.class checkAccessForPhoto:^(BOOL enabled) {
        if (enabled) {
            [self.manager setType:HXPhotoManagerSelectedTypeVideo];
            self.finishedImage = completion;
            HXCustomNavigationController * vc = [self picker];
            vc.isCamera = YES;
            [viewController presentViewController:vc animated:YES completion:nil];
        }else {
        }
    }];
}


+ (void)checkAccessForPhoto:(void (^)(BOOL))completionHandler
{
    PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
    if (authorizationStatus == PHAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (completionHandler) {
                    completionHandler(granted);
                }
            });
        }];
    } else if (authorizationStatus == PHAuthorizationStatusAuthorized) {
        if (completionHandler) {
            completionHandler(YES);
        }
    } else {
        if (completionHandler) {
            completionHandler(NO);
        }
    }
}

+ (void)checkAccessForCamera:(void (^)(BOOL))completionHandler
{
    AVAuthorizationStatus authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authorizationStatus == AVAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (completionHandler) {
                    completionHandler(granted);
                }
            });
        }];
    } else if (authorizationStatus == AVAuthorizationStatusAuthorized) {
        if (completionHandler) {
            completionHandler(YES);
        }
    } else {
        if (completionHandler) {
            completionHandler(NO);
        }
    }
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info
{
    if (info[UIImagePickerControllerEditedImage] != nil || info[UIImagePickerControllerOriginalImage] != nil) {
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            
        }else {
            
        }
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)photoNavigationViewControllerDidCancel:(HXCustomNavigationController *)photoNavigationViewController
{
    [photoNavigationViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)photoNavigationViewControllerFinishDismissCompletion:(HXCustomNavigationController *)photoNavigationViewController
{
    NSLog(@"%@",self.manager.afterSelectedPhotoArray);
}

- (void)photoNavigationViewController:(HXCustomNavigationController *)photoNavigationViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original
{
    if (photoNavigationViewController.manager.type == HXPhotoManagerSelectedTypePhoto) {
        if (photoList.count > 0) {
            HXPhotoModel * model = photoList.firstObject;
            if (self.finishedImage) {
                self.finishedImage(model);
            }
        }
    }
    if (photoNavigationViewController.manager.type == HXPhotoManagerSelectedTypeVideo) {
        if (videoList.count > 0) {
            HXPhotoModel * model = videoList.firstObject;
            if (self.finishedImage) {
                self.finishedImage(model);
            }
        }
    }
    
}

@end
